/* AppController.m
 *
 * Sets up an EOController and a DataSource objects which are not based on an underlying
 * RDBMS.
 * 
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 *
 * 
 */

#import <eoaccess/eoaccess.h>
#import <eointerface/eointerface.h>
#import<foundation/NSString.h>
#import <foundation/NSException.h>

#import "AppController.h"
#import "TableDataSource.h"

@implementation AppController


#define EXAMPLE_MODEL @"example"
#define PRODUCT_ENTITY @"Product"
#define DATABASE @"DATABASE"

void MyTopLevelErrorHandler(NXHandler *errorState);

- awakeFromNib {
	NSString *modelPath = [EOModel findPathForModelNamed:EXAMPLE_MODEL];
	EOEntity *productEntity;
	TableDataSource *masterSource;
	EOModel *model;
	NSString *databasePath;

	
	NXSetTopLevelErrorHandler(MyTopLevelErrorHandler);
	
	if (!modelPath) {
		NSString *emess = [NSString stringWithFormat:@"Cannot find path for model %@", EXAMPLE_MODEL];
		NXRunAlertPanel("ERROR", [emess cString], "OK", NULL, NULL);
		return self;
	}

	// Get the Product entity from the model. We are in trouble if it is not there!!!
	model = [[EOModel alloc] initWithContentsOfFile:modelPath];
	[model autorelease];
	if ( !(productEntity = [model entityNamed:PRODUCT_ENTITY]) ) {
		NSString *emess = [NSString stringWithFormat:@"Cannot find entity %@ in model %@", PRODUCT_ENTITY,  EXAMPLE_MODEL];
		NXRunAlertPanel("ERROR", [emess cString], "OK", NULL, NULL);
		return self;
	}

	// Instantiate a TableDataSource for the product entity and connect
	// the product controller to it.
	// The detail item data source will be automatically created and attached to
	// the detail controller (look at the qualified association set in IB
	// that connects the master controller (Product) to its detail (Item).
	// The association key is @"toItem"). That's the only thing that needs to be done, 
	// et voila...
	databasePath = [NSString stringWithFormat:@"%@/../%@", [self applicationPath], DATABASE];
	masterSource = [[TableDataSource alloc] initWithEntity:productEntity tablePath:databasePath];
	[masterSource autorelease];
	if (!masterSource) {
		NSString *emess = [NSString stringWithCString:"Cannot access the flat file database!!! You need to set the absolute path to the flat file db (AppController.m) or run the application from the example directory..."];
		NXRunAlertPanel("ERROR", [emess cString], "OK", NULL, NULL);
		return self;
	}
	[productController setDataSource:masterSource];
	[productController fetch:self];
	return self;
}

void MyTopLevelErrorHandler(NXHandler *errorState)
{
 	if (errorState->code >= NSExceptionBase &&
	   errorState->code <= NSLastException) {
	   NSException *exception = (id) errorState->data1;

	 NSLog(@"%@: %@\n", [exception exceptionName], [exception exceptionReason]);
	    }
}

#undef EXAMPLE_MODEL
#undef PRODUCT_ENTITY
#undef DATABASE

- (NSString *)applicationPath {
	int           i;
	NSString   *appPathName = nil;

	for (i = strlen(NXArgv[0]) - 1; (i >= 0) && (NXArgv[0][i] != '/'); i--);
	appPathName = [NSString stringWithCString:NXArgv[0] length:i];
	if (NXArgv[0][0] != '/') {
		char path[MAXPATHLEN+1];
		appPathName = [NSString stringWithFormat:@"%s/%@", getwd(path), appPathName];
	}
	return appPathName;
}


@end
